﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TOOL
{
    /// <summary>
    /// 处理Config文件
    /// </summary>
   public  class TConfig
    {
        /// <summary>
        /// 获取appSettings里的内容
        /// </summary>
        /// <param name="appKey">键名称</param>
        /// <returns></returns>
        public static string getConfigForAppSettings(string appKey)
        {
            return ConfigurationManager.AppSettings[appKey].ToString();
        }
        /// <summary>
        /// 获取connectionStrings里的内容
        /// </summary>
        /// <param name="appKey">键</param>
        /// <returns></returns>
        public static string getConfigForConnectionStrings(string appKey)
        {
            return ConfigurationManager.ConnectionStrings[appKey].ConnectionString;
        }
        /// <summary>
        /// 设置appSettings里的内容
        /// </summary>
        /// <param name="appKey">键</param>
        /// <param name="appKeyValue">值</param>
        public static void setConfigForAppSettings(string appKey, string appKeyValue)
        {
            bool flag = false;
            foreach (string str in ConfigurationManager.AppSettings)
            {
                if (string.Compare(str, appKey) == 0)
                {
                    flag = true;
                }
            }
            System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (flag)
            {
                configuration.AppSettings.Settings.Remove(appKey);
            }
            configuration.AppSettings.Settings.Add(appKey, appKeyValue);
            configuration.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
        }
        /// <summary>
        /// 设置connectionStrings里的内容
        /// </summary>
        /// <param name="appKey">键</param>
        /// <param name="appKeyValue">值</param>
        public static void setConfigForConnectionStrings(string appKey, string appKeyValue)
        {
            bool flag = false;
            for (int i = 0; i < ConfigurationManager.ConnectionStrings.Count; i++)
            {
                if (string.Compare(appKey, ConfigurationManager.ConnectionStrings[i].Name) == 0)
                {
                    flag = true;
                }
            }
            System.Configuration.Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            if (flag)
            {
                configuration.ConnectionStrings.ConnectionStrings.Remove(appKey);
            }
            ConnectionStringSettings settings = new ConnectionStringSettings(appKey, appKeyValue);
            configuration.ConnectionStrings.ConnectionStrings.Add(settings);
            configuration.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("connectionStrings");
        }
    }
}
